package com.wf.course.service.impl;

import com.utils.page.PageBean;
import com.wf.course.dao.IDistributionMapper;
import com.wf.course.model.comm.Distribution;
import com.wf.course.service.IDistributionService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;

/**
 * @Auther: liu
 * @Date: 2018/7/13 14:48
 * @Description: 分销
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IDistributionServiceImpl implements IDistributionService {

    private static Logger logger = LoggerFactory.getLogger(IDistributionServiceImpl.class);

    @Autowired
    private IDistributionMapper distributionMapper;

    @Override
    public void record(PageBean<Distribution> pageBean) {
        pageBean.setResults(distributionMapper.recordList(pageBean));
        pageBean.setTotalCount(distributionMapper.recordCount(pageBean));
    }

    @Override
    public void goodsList(PageBean<Distribution> pageBean) {
        pageBean.setResults(distributionMapper.goodsList(pageBean));
        pageBean.setTotalCount(distributionMapper.goodsCount(pageBean));
    }

    @Override
    public Integer deleteGoods(Integer id) {
        return distributionMapper.deleteGoods(id);
    }

    @Override
    public Distribution getGoodById(Integer id) {
        return distributionMapper.getGoodById(id);
    }

    @Override
    public void saveOrUpdateDistribution(Distribution distribution){
        distributionMapper.saveOrUpdateDistribution(distribution);
    }

    @Override
    public Integer addGoods(Distribution distribution) {
        return distributionMapper.addGoods(distribution);
    }

    @Override
    public Distribution configuration() {
        Distribution distribution = new Distribution();
        distributionMapper.configuration().stream().forEach(item -> {
            switch (item.get("key").toString()) {
                case "commission_rate":
                    distribution.setCommissionRate(Integer.valueOf(item.get("value").toString()));
                    break;
                case "high_commission_rate":
                    distribution.setHighCommissionRate(Integer.valueOf(item.get("value").toString()));
                    break;
                case "invite_commission_rate":
                    distribution.setInviteCommissionRate(Integer.valueOf(item.get("value").toString()));
                    break;
                case "condition":
                    distribution.setCondition(Integer.valueOf(item.get("value").toString()));
                    break;
                case "num":
                    distribution.setNum(Integer.valueOf(item.get("value").toString()));
                    break;
                case "h5_notice":
                    distribution.setH5_notice(item.get("remark").toString());
                    break;
                case "ios_notice":
                    distribution.setIos_notice(item.get("remark").toString());
                    break;
                case "android_notice":
                    distribution.setAndroid_notice(item.get("remark").toString());
                    break;
            }
        });
        return distribution;
    }

    @Override
    public void configure(Distribution distribution) {
        try {
            for (Field f : distribution.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                if (null != f.get(distribution) && StringUtils.isNotBlank(f.get(distribution).toString())) {
                    String key = f.getName();
                    Long time = System.currentTimeMillis() / 1000;
                    switch (f.getName()) {
                        case "commissionRate":
                            key = "commission_rate";
                            break;
                        case "highCommissionRate":
                            key = "high_commission_rate";
                            break;
                        case "inviteCommissionRate":
                            key = "invite_commission_rate";
                            break;
                    }
                    if (key.equals("h5_notice") || key.equals("ios_notice") || key.equals("android_notice")) {
                        distributionMapper.updateRemark(key, f.get(distribution).toString(), time.intValue());
                    } else {
                        distributionMapper.configure(key, Integer.valueOf(f.get(distribution).toString()), time.intValue());
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
